<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <ul id="oul"></ul>


    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.js"></script>
    <script>
        const oul = document.getElementById("oul")

        axios.interceptors.request.use((config) => {
            config.headers.token = sessionStorage.getItem("token")? sessionStorage.getItem("token"): ""
            return config
        })

        function init() {
            axios.get("/getGoods").then(res => {
                oul.innerHTML = ""
                if (res.data.data) {
                    res.data.data.forEach(item => {
                        const oli = document.createElement("li")
                        oli.innerHTML = `
                        ${item.title} ---
                        <button onclick="del(${item.id})">删除</button>
                    `
                        oul.appendChild(oli)
                    })
                }
            })
        }

        const del = (id) => {
            const token = sessionStorage.getItem("token")
            axios.delete(`/delGoods/${id}`).then(res => {
                console.log(res)
                alert(res.data.message)
                if (res.data.code == 0) {
                    init()
                }
            })
        }

        init()


    </script>
</body>

</html>        